<template>
  <div class="modal-wrapper">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item prop="parameter">
        <el-input v-model="form.parameter" type="textarea" :autosize="{ minRows: 10 }"></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer-account">
      <el-button type="primary" @click="save">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>
<script>
import isJSON from '@/utils/is-json';
export default {
  props: {
    parameter: {
      type: String,
      default: ''
    },
  },
  computed: {

  },
  data () {
    return {
      form: {
        parameter: ''
      },
      rules: {
        parameter: { required: true, validator: this.checkParameter, trigger: 'blur' }
      },
      loading: false,
    }
  },
  methods: {
    show () {
      // console.log('this.parameter', this.parameter);
      this.$refs.form.resetFields();
      this.form.parameter = this.parameter ? JSON.stringify(JSON.parse(this.parameter), null, 2) : '';
    },
    save () {
      if (!this.$common.doSubmit('form', this)) return;
      const result = JSON.stringify(JSON.parse(this.form.parameter));
      this.$emit('submit', result);
      this.$emit('close');
    },
    checkParameter (rule, value, callback) {
      if (!value) {
        callback(new Error('请输入算法参数'));
      } else {
        const flag = isJSON(value);
        if (!flag) {
          callback(new Error('请输入JSON格式的算法参数'));
        } else {
          callback();
        }
      }
    },
    cancel () {
      this.$emit('close');
    }
  }
}
</script>
